<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="author" content="cdy">
    <title>购物车</title>
    <script src="../../js/vue.js"></script>
    <script src="../../js/axios.js"></script>
</head>
<style>
    * {
        padding: 0;
        margin: 0;
        list-style: none;
    }

    .buyCar {
        width: 800px;
        /* height: 40px; */
        margin: 50px auto;
    }

    .tr {
        overflow: hidden;
        display: flex;
        justify-content: space-evenly;
        /* align-items: center; */
        background-color: #000;
        border-top: 1px solid #f1f1f1;
    }

    .item {
        color: wheat;
        text-align: center;
        line-height: 80px;
        float: left;
        width: 100px;
        height: 80px;
        flex: 1;
        padding: 5px;
        /* box-sizing: border-box; */
    }

    .img {
        width: 100%;
        height: 100%;
    }

    .tr button {
        width: 20px;
        height: 20px;
        vertical-align: middle;
    }

    .tr button span {
        display: block;
    }

    .tr input {
        width: 20px;
        height: 20px;
        vertical-align: middle;
        text-align: center;
        outline: none;
    }

    .tr .input {
        width: 25px;
        height: auto;
    }

    .name {
        height: 40px;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        text-overflow: ellipsis;
        line-height: normal;
        white-space: pre-wrap;
        word-wrap: break-word;
    }

    .del {
        cursor: pointer;
    }

    .del:hover {
        color: tomato;
    }

    .box {
        width: 150px;
    }
</style>

<body>
    <div id="app">
        <div class="buyCar">
            <div class="tr">
                <div class="item">
                    <label><input type="checkbox" @change="checkall" :checked="checkAll">全选</label>
                </div>
                <div class="item">商品图片</div>
                <div class="item">商品描述</div>
                <div class="item">商品价格</div>
                <div class="item">商品数量</div>
                <div class="item">小计</div>
                <div class="item">操作</div>
            </div>
            <div class="tr" v-for="item in list" :key="item.id">
                <div class="item">
                    <input type="checkbox" v-model="item.checked">
                </div>
                <div class="item"><img :src="item.goodsimg" class="img"></div>
                <div class="item name">{{item.goodsname}}</div>
                <div class="item">{{item.goodsprice}}</div>
                <div class="item">
                    <button v-if="item.buynum>1" @click="calc(item,-1)">-</button>
                    <button v-else>-</button>
                    <!-- <button><span v-show="item.buynum>1" @click="calc(item,-1)">-</span></button> -->
                    <input class="input" type="text" v-model="item.buynum" @input="input(item.buynum)">
                    <button @click="calc(item,1)">+</button>
                </div>
                <div class="item">{{(item.goodsprice*item.buynum).toFixed(2)}}</div>
                <div class="item del" @click="del(item.id)">删除</div>
            </div>
            <div class="tr">
                <div class="item del box" @click="cler">批量删除</div>
                <div class="item del box" @click="reverse">反选</div>
                <div class="item"></div>
                <div class="item box">总量：
                    <span>{{allnum}}</span>
                </div>
                <div class="item box">总价：
                    <span>{{allprice}}</span>
                </div>
            </div>
        </div>
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            checkAll: false,
            inputReg: /^[1-9][\d]{0,1}$/,
            allnum: 0,
            allprice: 0.00,
            list: []
        },
        async created() {
            // let str = location.search;
            let userid = window.localStorage.getItem("userinfo");
            if (userid) { //表示有商品id并且登录了
                // let goodsid = str.split("=")[1];
                //根据商品id获取数据
                let {data: list} = await axios({
                    method: "get",
                    url: "http://localhost:2345/shoppingcar",
                    params: {
                        userid
                    }
                });

                console.log(list);
                this.list = list;


            } else {
                location.href = "goodslist.html";
            }
        },
        methods: {
            async calc(item, n) { //加减
                let id = item.id; //获取id
                let buynum = item.buynum * 1 + n;
                await axios({
                    method: "patch",
                    url: "http://localhost:2345/shoppingcar/" + id, //根据id修改buynum数量
                    data: {
                        buynum
                    }
                });

                item.buynum = buynum;
            },
            async del(id) { //单删
                console.log(id);
                if (confirm("确定要删除该商品吗？")) {
                    await axios({
                        method: "delete",
                        url: "http://localhost:2345/shoppingcar/" + id,
                    });

                    this.list = this.list.filter(item => item.id != id);
                }
            },
            checkall() { //全选
                let flag = window.event.target.checked;
                this.list.forEach(item => item.checked = flag);
                this.checkAll = flag;
            },
            async cler() { //全部删除
                let list = this.list.filter(item => item.checked);
                // console.log(list);

                // list.forEach(async item => { //数据库删除，把选中的放到列表中循环通过id删除
                //     await axios({
                //         method: "delete",
                //         url: "http://localhost:2345/shoppingcar/" + item.id,
                //     });
                // });

                for (let i = 0; i < list.length; i++) {
                    let id = list[i].id;
                    await axios({
                        method: "delete",
                        url: "http://localhost:2345/shoppingcar/" + id,
                    });
                }

                this.list = this.list.filter(item => !item.checked); //页面删除，把没选中的放到列表中再渲染一遍
            },
            reverse() { //点击反选
                this.list.forEach(item => item.checked = !item.checked);
            }
        },
        watch: {
            list: {
                deep: true, //深度监听
                handler(newList) {
                    var n = 0;
                    var p = 0;
                    if (newList.length) { //有长度在判断

                        newList.forEach(async function (item) {//反选
                            await axios({
                                method: "patch",
                                url: "http://localhost:2345/shoppingcar/" + item.id, //根据id修改chenck状态
                                data: {
                                    checked: item.checked
                                }
                            });
                        });

                        this.checkAll = newList.every(item => item.checked); //反选

                        let checkedList = newList.filter(item => item.checked); //找到选中的集合
                        // console.log(checkedList);

                        if (checkedList) { //有选中的集合 就计算

                            checkedList.forEach(item => { //所有数量和价格
                                n += item.buynum * 1;
                                p += (item.goodsprice * item.buynum);
                            })
                        }

                    } else {
                        this.checkAll = false;
                    }

                    this.allnum = n;
                    this.allprice = p.toFixed(2);
                }
            }
        }
    })
</script>

</html>